# 自定义分页
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response


class DefaultPagination(PageNumberPagination):
    # 分页页码参数名称
    page_query_param = 'page'
    # 分页条数参数名称
    page_size_query_param = 'limit'
    # 默认分页条数
    page_size = 10
    # 最大分页条数
    max_page_size = 100

    # 覆盖父级的方法
    def get_paginated_response(self, data):
        return Response({
            'page': int(self.get_page_number(self.request, self.page.paginator)) or 1,
            'total': self.page.paginator.count if self.page else 0,
            'limit': int(self.get_page_size(self.request)) or self.page_size,
            'is_next': self.page.has_next(),
            'is_previous': self.page.has_previous(),
            'items': data,
        })
